package com.converters;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.inject.Inject;
import javax.inject.Named;

import com.beans.TasksTableBean;
import com.controllers.EmployeeController;
import com.model.Employee;

@Named
public class EmployeeConverter implements Converter {
	
	public EmployeeConverter(){};
	
	@Inject
    public EmployeeConverter(EmployeeController employeeController) {
    }

	public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            try {
            	for (Employee employee : TasksTableBean.employees) {
            		
            		if (employee.toString().equals( submittedValue)) {
                        return employee;
                    }
                }

            } catch(NumberFormatException exception) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid Employee"));
            }
        }

        return null;
    }

    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        if (value == null) {
            return null;
        } else {
            return String.valueOf((value));
        }
    }
}
